/*!
 * This file implements a form to display the album informations.
 * @author Mazzer Yannis, Calingarayar Martial
 * @file album.h
 */
#include <QString>
#include <QTime>
#include <QDate>
#include <QMap>
#include <QSqlTableModel>
#include <QSqlRecord>
#include "album.h"
#include "ui_albumdescr.h"


Album::Album(QWidget *parent) : QWidget(parent)
{
    this->draw();
}

int Album::getNumero()
{
    return this->numero;
}

QString Album::getTitle()
{
    return this->titleLineEdit->text();
}

QString Album::getArtist()
{
    return this->artistLineEdit->text();
}

QTime Album::getRunningTime()
{
    return this->lengthTimeEdit->time();
}

QDate Album::getReleaseDate()
{
    return this->dateDateEdit->date();
}

QString Album::getLabel()
{
    return this->labelLineEdit->text();
}

QString Album::getComposer()
{
    return this->composerLineEdit->text();
}

QString Album::getProducer()
{
    return this->producerLineEdit->text();
}

//int Album::getTrackCount()
//{
//    return this->tracks.size();
//}
//
//QString Album::getTrackName(const int &trackNumber)
//{
//    return this->tracks.value(trackNumber);
//}

int Album::getIndex()
{
    return this->mapper->currentIndex();
}

void Album::setModelIndex(const QModelIndex &index, QSqlTableModel *model)
{
    this->mod = model;
    this->record = model->record(index.row());
    setNumero(this->record.value("numero").value<int>());

    this->mapper = new QDataWidgetMapper(this);
    this->mapper->setModel(model);
    this->mapper->addMapping(this->titleLineEdit,
			     model->fieldIndex("Title"));
    this->mapper->addMapping(this->artistLineEdit,
			     model->fieldIndex("Artist"));
    this->mapper->addMapping(this->lengthTimeEdit,
			     model->fieldIndex("RunningTime"));
    this->mapper->addMapping(this->dateDateEdit,
			     model->fieldIndex("RelDate"));
    this->mapper->addMapping(this->labelLineEdit,
			     model->fieldIndex("Label"));
    this->mapper->addMapping(this->composerLineEdit,
			     model->fieldIndex("Composer"));
    this->mapper->addMapping(this->producerLineEdit,
			     model->fieldIndex("Producer"));
    this->mapper->setCurrentIndex(index.row());

    connect(model, SIGNAL(modelReset()), this, SLOT(resetIndex()));
    connect(this->commitPushButton, SIGNAL(clicked()),
	    model, SLOT(submitAll()));
}

void Album::trackSetUp()
{
    trackModel = new QSqlTableModel(this, mod->database());
    trackModel->setTable("Tracks");
    QString filter = QString("numero = %1").arg(numero);
    trackModel->setFilter(filter);
    trackModel->select();
    this->tracksTreeView->setModel(trackModel);
    this->tracksTreeView->hideColumn(0);
    this->tracksTreeView->hideColumn(1);
    this->tracksTreeView->hideColumn(4);
}

void Album::setNumero(const int &newNumero)
{
    this->numero = newNumero;
}

void Album::setTitle(const QString &newTitle)
{
    this->titleLineEdit->setText(newTitle);
    this->record.setValue("Title",QVariant(newTitle));
}

void Album::setArtist(const QString &newArtist)
{
    this->artistLineEdit->setText(newArtist);
    this->record.setValue("Artist", QVariant(newArtist));
}

void Album::setRunningTime(const QTime &newRunningTime)
{
    this->lengthTimeEdit->setTime(newRunningTime);
    this->record.setValue("RunningTime", QVariant(newRunningTime));
}

void Album::setReleaseDate(const QDate &newReleaseDate)
{
    this->dateDateEdit->setDate(newReleaseDate);
    this->record.setValue("RelDate", QVariant(newReleaseDate));
}

void Album::setLabel(const QString &newLabel)
{
    this->labelLineEdit->setText(newLabel);
    this->record.setValue("Label", QVariant(newLabel));
}

void Album::setComposer(const QString &newComposer)
{
    this->composerLineEdit->setText(newComposer);
    this->record.setValue("Composer", QVariant(newComposer));
}

void Album::setProducer(const QString &newProducer)
{
    this->producerLineEdit->setText(newProducer);
    this->record.setValue("Producer", newProducer);
}

//void Album::addOrModTrack(const int &trackNumber, const QString &trackName)
//{
//    this->tracks.insert(trackNumber, trackName);
//}
//
//void Album::delTrack(const int &trackNumber)
//{
//    this->tracks.remove(trackNumber);
//}
//
//void Album::delTrack(const QString &trackName)
//{
//    this->tracks.remove(this->tracks.key(trackName));
//}

void Album::draw()
{
    setupUi(this);
}

void Album::resetIndex()
{
    for(int i = 0; i <= this->mod->rowCount(); i++) {
	QVariant num = this->mod->record(i).value("numero");
	if(num == this->record.value("numero")) {
	    this->mapper->setCurrentIndex(i);
	    break;
	}
    }
}
